package jehc.cloud.gen.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.cloud.common.annotation.NeedLoginUnAuth;
import jehc.cloud.common.base.BasePage;
import jehc.cloud.common.base.BaseResult;
import jehc.cloud.common.base.BaseSearch;
import jehc.cloud.common.util.StringUtil;
import jehc.cloud.gen.base.BaseAction;
import jehc.cloud.gen.model.XtScript;
import jehc.cloud.gen.service.XtScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageInfo;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Desc 平台脚本
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtScript")
@Api(value = "平台脚本API",description = "平台脚本API")
public class XtScriptController extends BaseAction {
    @Autowired
    private XtScriptService xtScriptService;

    /**
     * 列表页面
     * @param model
     * @return
     */
    @ApiOperation(value="列表页面", notes="列表页面")
    @RequestMapping(value="/pageList")
    public ModelAndView pageList(Model model) {
        return new ModelAndView("xt-script/xt-script-list");
    }

    /**
     * 新增页面
     * @param model
     * @return
     */
    @ApiOperation(value="新增页面", notes="新增页面")
    @RequestMapping(value="/pageAdd")
    public ModelAndView pageAdd(Model model) {
        return new ModelAndView("xt-script/xt-script-add");
    }

    /**
     * 编辑页面
     * @param model
     * @param xt_script_id
     * @return
     */
    @ApiOperation(value="编辑页面", notes="编辑页面")
    @RequestMapping(value="/pageUpdate")
    public ModelAndView pageUpdate(Model model,String xt_script_id) {
        model.addAttribute("xt_script_id",xt_script_id);
        return new ModelAndView("xt-script/xt-script-update");
    }

    /**
     * 详情页面
     * @param model
     * @param xt_script_id
     * @return
     */
    @ApiOperation(value="详情页面", notes="详情页面")
    @RequestMapping(value="/pageDetail")
    public ModelAndView pageDetail(Model model,String xt_script_id) {
        model.addAttribute("xt_script_id",xt_script_id);
        return new ModelAndView("xt-script/xt-script-detail");
    }

    /**
     * 分页查询
     * @param baseSearch
     */
    @NeedLoginUnAuth
    @PostMapping(value="/list")
    @ApiOperation(value="分页查询", notes="分页查询")
    @ResponseBody
    public BasePage getXtScriptListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<XtScript> xt_ScriptList = xtScriptService.getXtScriptListByCondition(condition);
        PageInfo<XtScript> page = new PageInfo<XtScript>(xt_ScriptList);
        return outPageBootStr(page,baseSearch);
    }

    /**
     * 单条记录
     * @param xt_script_id
     */
    @NeedLoginUnAuth
    @GetMapping(value="/get/{xt_script_id}")
    @ApiOperation(value="查询单个平台脚本", notes="查询单个平台脚本")
    @ResponseBody
    public BaseResult getXtScriptById(@PathVariable("xt_script_id")String xt_script_id){
        XtScript xt_Script = xtScriptService.getXtScriptById(xt_script_id);
        return outDataStr(xt_Script);
    }

    /**
     * 添加
     * @param xtScript
     */
    @PostMapping(value="/add")
    @ApiOperation(value="创建单个平台脚本", notes="创建单个平台脚本")
    @ResponseBody
    public BaseResult addXtScript(@RequestBody XtScript xtScript){
        int i = 0;
        if(null != xtScript){
            xtScript.setXt_script_id(toUUID());
            xtScript.setXt_script_ctime(getSimpleDateFormat());
            xtScript.setXt_userinfo_id(getXtUid());
            i=xtScriptService.addXtScript(xtScript);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 修改
     * @param xtScript
     */
    @PutMapping(value="/update")
    @ApiOperation(value="编辑单个平台脚本", notes="编辑单个平台脚本")
    @ResponseBody
    public BaseResult updateXtScript(@RequestBody XtScript xtScript){
        int i = 0;
        if(null != xtScript){
            xtScript.setXt_script_mtime(getSimpleDateFormat());
            xtScript.setXt_userinfo_id(getXtUid());
            i=xtScriptService.updateXtScript(xtScript);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }
    /**
     * 删除
     * @param xt_script_id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除平台脚本", notes="删除平台脚本")
    @ResponseBody
    public BaseResult delXtScript(String xt_script_id){
        int i = 0;
        if(!StringUtil.isEmpty(xt_script_id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("xt_script_id",xt_script_id.split(","));
            i=xtScriptService.delXtScript(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 获取集合
     * @param xtScript
     */
    @NeedLoginUnAuth
    @GetMapping(value="/all/list")
    @ApiOperation(value="获取集合", notes="获取集合")
    @ResponseBody
    public BaseResult getXtScriptList(XtScript xtScript){
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("xt_script_type",1);
        condition.put("xt_script_status",0);
        return outComboDataStr(xtScriptService.getXtScriptListByCondition(condition));
    }

}

